package com.xx;

import com.xx.model.Smartphone;
import com.xx.pojo.ConnectionParam;
import com.xx.util.ConnectionUtil;
import com.xx.util.HibernateHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class SchemaTranslator {


    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {

        String config_file_path = System.getProperty("config_file_path");
        if (config_file_path == null) {
            System.out.println("请添加 -Dconfig_file_path=xxx.properties VM参数");
        } else {
            System.out.println(String.format("使用配置文件：%s", config_file_path));

            Properties ps = new Properties();
            ps.load(new FileInputStream(config_file_path));

            // 连接源数据库
            Connection connection = ConnectionUtil.getConnection(ps);
            System.out.println(connection);

            // 获得目标数据源的所有表结构

            // loop 将表结构转成java代码

            // 类加载器加载

            // 调用hibernate方法生成ddl


            HibernateHelper translator = new HibernateHelper();
            Class[] entityClasses = {Smartphone.class};
            translator.setDialect("org.hibernate.dialect.MySQLDialect")
                    .addAnnotatedClasses(entityClasses)
                    .translate(new FileOutputStream(new File("db-schema.sql")));
        }
    }
}
